package com.mty.mydemo.View;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import java.util.ArrayList;
import java.util.List;



/**
 * @ClassName:${class}
 * @Description：水波纹View
 * Created by ${mty} on 2016/3/14.
 * http://blog.csdn.net/yunyuliunian
 */
public class WaterRippleView extends View{
    private Paint paint;
    private List<Float> list;
    private int mWidth;
    private int mHeight;
    private boolean isDraw=false;//开关

    public WaterRippleView(Context context) {
        super(context);
        init();
    }

    public WaterRippleView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public WaterRippleView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }



    private void init() {
        list=new ArrayList<>();
        paint=new Paint();
        paint.setAntiAlias(false);
        paint.setStrokeWidth(5);
        paint.setStyle(Paint.Style.FILL);
        paint.setColor(Color.BLUE);
        list.add((float) 0);
        list.add((float) 0);
        list.add((float) 0);
        list.add((float) 0);
        list.add((float) 0);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if (list.get(4)<20 && list.get(3)==0){
            list.set(4,list.get(4)+1);
            canvas.drawCircle(mWidth/2,mHeight/2,list.get(4)/100*mWidth,paint);//画圆
        }
        for (int i = 4; i >= 0; i--) {
            if (i!=4 && list.get(i+1)>20){
                list.set(i,list.get(i)+1);
            }else if (i==4){
                if (list.get(i)==100){
                    list.remove(i);
                    list.add(0, (float) 0);
                }else {
                    list.set(i,list.get(i)+1);
                }
            }
            paint.setAlpha((int) (255 / 100 * (100 - list.get(i))));//控制透明度
            canvas.drawCircle(mWidth/2,mHeight/2,list.get(i)/100*mWidth/2,paint);//画圆
        }
        if (isDraw){
            postInvalidateDelayed(16);
        }else {//画个白色遮盖  然后重置数值
            paint.setColor(Color.WHITE);
            canvas.drawRect(0,0,mWidth,mHeight,paint);
            paint.setColor(Color.BLUE);
            list.clear();
            list.add((float) 0);
            list.add((float) 0);
            list.add((float) 0);
            list.add((float) 0);
            list.add((float) 0);
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mWidth=w;
        mHeight=h;
    }


    public boolean isDraw() {
        return isDraw;
    }

    public void setDraw(boolean draw) {
        isDraw = draw;
    }

    //
    //WaterRippleView view= (WaterRippleView) findViewById(R.id.myview);
    //view.setOnClickListener(new View.OnClickListener() {
    //    @Override
    //    public void onClick(View v) {
    //        if (((WaterRippleView)v).isDraw()){
    //            ((WaterRippleView)v).setDraw(false);
    //        }else {invalidate
    //            ((WaterRippleView)v).setDraw(true);
    //            v.invalidate();
    //        }
    //    }
    //});
}
